<?php
class CPoint
{
	public $user_id;
	public $definitions = array();
	
	//Danh cho nguoi mua
	const EXPEND_POINT = 1; //Doanh số
	const PRODUCT_POINT = 2; //Sản phẩm
	const SIGN_IN_POINT = 3; //Đăng nhập
	const COMMENT_POINT = 4; //Comment
	
	//Danh cho nguoi ban
	const INCOME_POINT = 10; //Thu nhập
	const TURN_OVER_POINT = 11; //Doanh số
	const PRODUCT_SOLD_POINT = 12; //Số sản phẩm đã bán
	const SUPPORT_POINT = 13;//Điểm phục vụ
	const INTRODUCTION_POINT = 14; //Điểm giới thiệu thêm người
	const PRESTIGE_POINT = 15; //Điểm uy tín
	
	function __construct($user_id = false)
	{
		if(!$user_id)
		{
			$user_id = user()->id;			
		}
		$this->user_id = $user_id;
	}
	function add($type, $parameters ='')
	{
		//XU LY NGUOI BAN: CAP NHAT TAT CA NGUOI BAN		
		if($type>=10)
		{			
			if($this->user_id and $user = CTable('user')->select(array(id=>$this->user_id)) and $point = $this->get_defined_point($type, $parameters))
			{
				//Dem xem count ung voi tung type la bao nhieu?
				$seller_current=CTable('estore_seller')->select(array('seller_id'=>$this->user_id));				
				//file_put_contents(DEBUG_DIR.'log.txt',$seller_current['total_success_amount']);exit();
				switch($type)
				{
					case 10: //Thu nhập
						$point=($point*$seller_current['total_success_amount']);	//He so co so: 1K la 1 Diem
						break;
						//file_put_contents(DEBUG_DIR.'log.txt',$point);exit();
					case 11: //Doanh số					
						$point=($point*$seller_current['total_order_amount']);		//He so co so: 1K la duoc 0.01 Diem
						break;
					case 12: //Số sản phẩm đã bán
						$point=($point*$seller_current['total_sucess_product']); 	//He so co so: 1 sp duoc 1 Diem
						break;
					case 13://Phục vụ
						$point=($point*$seller_current['customer_support_count']);	//He so co so: phuc vu mot nguoi duoc 0.4 Diem
						break;
					case 14: //Giới thiệu
						$point=($point*$seller_current['customer_introduc_count']);  //He so co so: gioi thieu mot nguoi duoc 1 Diem
						break;
				}				
				//file_put_contents(DEBUG_DIR.'log.txt',$point);exit();				
				$seller_point=CTable('point')->select(array('user_id'=>$this->user_id,'type'=>$type));
				//Trong cung mot thang thi update lai so diem
				if(!empty($seller_point))
				{
					CTable('point')->update(array(
						'point' => $point,
		 				'parameters' => $parameters
					),array('user_id'=>$this->user_id,'type'=>$type));
				}				
				//Khong cung mot thang thi them moi
				else
				{
					CTable('point')->save(array(
						'user_id' => $this->user_id,
						'type'=>$type,
						'point' => $point,
						'month' =>date('m'),
						'year' =>date('Y'),
		 				'parameters' => $parameters,
						'is_seller_point' =>1
					));
				}			
				//Cap nhat diem vao bang user (ma co type>=10)
				$seller_points=CTable('point')->select_all(EAnd(EEqual('user_id',EString($this->user_id)),EGT('type',9)));
				//file_put_contents(DEBUG_DIR.'log.txt',var_export($seller_points,true));exit();
				foreach ($seller_points as $seller_point)
				{
					$points+=$seller_point['point'];
				}
//				file_put_contents(DEBUG_DIR.'log.txt',$points);exit();
				//Cap nhat Level cho nguoi ban
				if($points < 40)
				{
					$level=0;
				}
				elseif($points >= 40 and $points < 190)
				{
					$level=1;
				}
				elseif($points >= 190 and $points < 650)
				{
					$level=2;
				}
				elseif($points >= 650 and $points < 2000)
				{
					$level=3;
				}
				elseif($points >= 2000)
				{
					$level=4;
				}
				
				CTable('user')->update(array(
						'seller_point' =>$points,
						'seller_level' =>$level
					),array('id'=>$this->user_id));
			}
		}		
		//XU LY BAN NGUOI MUA
		elseif($type<10) 
		{
			if($this->user_id and $user = CTable('user')->select(array(id=>$this->user_id)) and $point = $this->get_defined_point($type, $parameters))
			{
				//Dem xem count ung voi tung type la bao nhieu?
				$user_stats=CTable('user_stats')->select(array('user_id'=>$this->user_id,'type'=>$type,'month'=>date('m')));				
				$point=($point*$user_stats['count']);
				$user_point=CTable('point')->select(array('user_id'=>$this->user_id,'type'=>$type,'month'=>date('m')));
				//Trong cung mot thang thi update lai so diem
				if(!empty($user_point))
				{
					CTable('point')->update(array(
						'point' => $point,
		 				'parameters' => $parameters
					),array('user_id'=>$this->user_id,'type'=>$type));
				}
				
				//Khong cung mot thang thi them moi
				else
				{
					CTable('point')->save(array(
						'user_id' => $this->user_id,
						'type'=>$type,
						'point' => $point,
						'month' =>date('m'),
						'year' =>date('Y'),
		 				'parameters' => $parameters,
						'is_seller_point' =>0
					));
				}			
				//Cap nhat diem vao bang user
				$user_point_month=CTable('point')->select_all(EAnd(EEqual('user_id',EString($this->user_id)),ELT('type',9),EEqual('month',date('m'))));
				//file_put_contents(DEBUG_DIR.'log.txt',var_export($seller_points,true));exit();				
				foreach ($user_point_month as $point_month)
				{
					$points+=$point_month['point'];
				}
				//Cap nhat Level cho nguoi mua
				if(!$this->definitions)
				{
					$this->definitions = CXml('data/'.Portal::$default.'/point_definition.xml')->decode();
				}
				if($points < $this->definitions['buyer-level']['level1'])
				{
					$level=0;
				}
				elseif($points >= $this->definitions['buyer-level']['level1'] and $points < $this->definitions['buyer-level']['level2'])
				{
					$level=1;
				}
				elseif($points >= $this->definitions['buyer-level']['level2'] and $points < $this->definitions['buyer-level']['level3'])
				{
					$level=2;
				}
				elseif($points >= $this->definitions['buyer-level']['level3'] and $points < $this->definitions['buyer-level']['level4'])
				{
					$level=3;
				}
				elseif($points >= $this->definitions['buyer-level']['level4'])
				{
					$level=4;
				}				
				CTable('user')->update(array(
						'buyer_point' =>$points,
						'buyer_level' =>$level
					),array('id'=>$this->user_id));
			}			
		}	
	}
	function get_defined_point($type, $parameters)
	{
		
		if(!$this->definitions)
		{
			$this->definitions = CXml('data/'.Portal::$default.'/point_definition.xml')->decode();
		}
//		file_put_contents(DEBUG_DIR.'log.txt',var_export($this->definitions,true));exit();
		switch($type)
		{
			//Phan danh cho nguoi mua
			case CPoint::EXPEND_POINT: //Doanh số
				return $this->definitions['buyer-point']['expend'];
			case CPoint::PRODUCT_POINT: //Sản phẩm
				return $this->definitions['buyer-point']['product'];
			case CPoint::SIGN_IN_POINT: //Đăng nhập
				return $this->definitions['buyer-point']['sign-in'];
			case CPoint::COMMENT_POINT: //Comment
				return $this->definitions['buyer-point']['comment'];			
			//Phan danh cho nguoi ban	
			case CPoint::INCOME_POINT: //Thu nhập
				return $this->definitions['seller-point']['income'];		//He so co so: 1K la 1 Diem
			case CPoint::TURN_OVER_POINT: //Doanh số					
				return $this->definitions['seller-point']['turn-over'];		//He so co so: 1K la duoc 0.01 Diem
			case CPoint::PRODUCT_SOLD_POINT: //Số sản phẩm đã bán
				return $this->definitions['seller-point']['product-sold']; 	//He so co so: 1 sp duoc 1 Diem
			case CPoint::SUPPORT_POINT://Phục vụ
				return $this->definitions['seller-point']['support'];		//He so co so: phuc vu mot nguoi duoc 0.4 Diem
			case CPoint::INTRODUCTION_POINT: //Giới thiệu
				return $this->definitions['seller-point']['introduction'];  //He so co so: gioi thieu mot nguoi duoc 1 Diem
			case CPoint::PRESTIGE_POINT: //Diem uy tin
				return $this->definitions['seller-point']['prestige'];		//He so co so:
		}
		return 0;
	}
	//Method xu ly khi ma dang nhap, comment(moi ngay chi co mot lan dang nhap hoac comment duoc tinh)
	function stats($type, $count = 1, $parameters = '')
	{
		if($stats = CTable('user_stats')->select(array(
			'type' => $type,
			'user_id' => $this->user_id,
			'month' => date('m'),
			'year'=>date('Y')
		)))
		{
			if(!$parameters or ($parameters != $stats['parameters']))
			{
				CTable('user_stats')->update(array(
					'count' => $stats['count'] + $count,
					'parameters' => $parameters
				), $stats['id']);
			}
		}
		else {
			CTable('user_stats')->save(array(
				'user_id' => $this->user_id,
				'type' => $type,
				'count' => 1,
				'month' => date('m'),
				'year'=>date('Y'),
				'parameters' => $parameters
			));
		}
	}	

	//Method xu ly khi ma thuc hien don hang thanh cong 1 san pham/thang
	function success_point($type,$count=1,$parameters='')
	{
		$str=explode('/',$parameters);
		if($stats = CTable('user_stats')->select(array(
			'type' => $type,
			'user_id' => $this->user_id,
			'month' => date('m'),
			'year'=>date('Y')
		)))
		{
			//so san pham trong cung mot thang
			if($stats['month']==$str[1] and $stats['year']==$str[2])
			{
				CTable('user_stats')->update(array(
					'count' => $stats['count'] + $count,
					'parameters' => $parameters
				), $stats['id']);				
			}
			else
			{
				CTable('user_stats')->save(array(
				'user_id' => $this->user_id,
				'type' => $type,
				'count' => 1,
				'month' => date('m'),
				'year'=>date('Y'),
				'parameters' => $parameters
			));				
			}
		}
		else {
			CTable('user_stats')->save(array(
				'user_id' => $this->user_id,
				'type' => $type,
				'count' => 1,
				'month' => date('m'),
				'year'=>date('Y'),
				'parameters' => $parameters
			));
		}
		
	}	
}
function CPoint($user_id = false)
{
	return new CPoint($user_id);
}
?>